package com.cuit.riskassessment.action.supplier;


import java.io.File;

import java.util.Date;
import java.util.List;


import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.cuit.riskassessment.service.SupplierService;
import com.cuit.riskassessment.util.Result;
import com.cuit.riskassessment.vo.Supplier;

@Controller
@RequestMapping(value = "supplierAction")
public class SupplierAction implements ServletContextAware {
	@Resource(name = "supplierServiceObject")
	private SupplierService service;
	private ServletContext servletContext;

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;

	}

	@RequestMapping(value = "getSuppliers")
	public @ResponseBody
	List<Supplier> getSuppliers(String name) {
		if (name == null || name == "") {
			return service.getAllSuppliers();
		}
		return service.getSupplierByName(name);
	}

	@RequestMapping(value = "addOrUpdate")
	public @ResponseBody
	Object addOrUpdate(HttpServletResponse response, Supplier supplier) {
		response.setHeader("Accept", "application/json");
		String message = new String("操作成功");
		Result res = new Result();
		if (supplier.getId() == null || supplier.getId() == "") {
			service.addOrUpdateSupplier(supplier, true);
			res.setSuccess(true);
			message = new String("添加成功");
		} else {
			service.addOrUpdateSupplier(supplier, false);
			res.setSuccess(true);
			message = new String("更新成功");
		}
		res.setMessage(message);
		return res;
	}

	@RequestMapping(value = "del")
	public @ResponseBody
	Object del(HttpServletResponse response, String id) {
		response.setHeader("Accept", "application/json");
		Supplier supplier = new Supplier();
		supplier.setId(id);
		if (service.isHaveFile(supplier).equals("")
				|| service.isHaveFile(supplier) == null) {// 删除文件

		} else {
			try {
				String littleName = service.isHaveFile(supplier);
				littleName = littleName.substring(21, littleName.length() - 10);
				String path = servletContext.getRealPath("/upload");
				File delFile = new File(path + "\\" + littleName);
				if (delFile.isFile() && delFile.exists()) {
					delFile.delete();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		String message = new String("操作成功");
		Result res = new Result();
		service.delSupplier(Integer.valueOf(id));
		res.setMessage(message);
		return res;
	}

	@RequestMapping(value = "file")
	public @ResponseBody
	Object file(HttpServletResponse response,@RequestParam("id") String id,
			@RequestParam("fileName") String fileName,
			@RequestParam("file") CommonsMultipartFile file) {
		response.setHeader("Content-Type", "text/html");
		if (file.getSize()>10000000) {//判断文件大小
			return "{success:false}";
		}
		try {
			String type = fileName.split("\\.")[1];
			Supplier supplier = new Supplier();
			supplier.setId(id);
			String name = new Date().getTime() + "." + type;
			// 将文件存入服务器
			if (!file.isEmpty()) {
				String path = servletContext.getRealPath("/upload");
				// 创建文件名
				File newfile = new File(path + "\\" + name);
					file.getFileItem().write(newfile);// 写入文件
			}
			// 判断服务器中是否有文件
			if (service.isHaveFile(supplier) == null
					|| service.isHaveFile(supplier).equals("")) {// 删除文件

			} else {
					String littleName = service.isHaveFile(supplier);
					littleName = littleName.substring(21, littleName.length() - "' target='_blank'>点击查看</a>".length());
					String path = servletContext.getRealPath("/upload");
					File delFile = new File(path + "\\" + littleName);
					if (delFile.isFile() && delFile.exists()) {
						delFile.delete();
					}
			}
			// 将路径添加到数据库
			String url = "<a href = '../upload/" + name + "' target='_blank'>点击查看</a>";
			supplier.setFile(url);
			service.setFilePath(supplier);
			return "{success:true}";
		} catch (Exception e) {
			return "{success:false}";
		}


	}
}
